package com.example.rocketmq.message;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Component
public class ProducerC {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer();
        producer.setNamesrvAddr("101.35.153.254:9876;121.5.23.139:9876;1.15.1.55:9876");
        producer.setProducerGroup("groupC");
        producer.start();
        for (int i = 0; i < 20; i++) {
            Message message = new Message();
            message.setTopic("topicC");
            message.setTags("tagsA");
            message.setBody(("消息A"+i).getBytes());
            SendResult sendResult = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object args) {
                    int id = (int) args;
                    int m = id % list.size();
                    return list.get(m);
                }
            }, i);
            System.out.println(sendResult);
        }

        producer.shutdown();
    }
}
